<?php
namespace app\store\controller\transform;

use app\store\controller\Controller;
use \think\Db;
/**
 * TAGS管理
 * Class User
 * @package app\store\controller
 */
class Tags extends Controller
{
    /**
     * 用户列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {

        return $this->fetch('index');
    }
	
	public function list(){
        $list = Db::name('tags_type')->select()->toArray();
        foreach($list as $key=>$vo){
            $list[$key]['tags'] = Db::name('tags_list')->where('typeid',$vo['id'])->select()->toArray();
        }

        $this->assign('list',$list);
		return $this->fetch('list');
	}
	
    public function add_type()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        if (Db::name('tags_type')->insert($this->postData('data'))) {
            return $this->renderSuccess('添加成功', url('transform.tags/list'));
        }
        return $this->renderError('添加失败');
    }

    public function add_tags()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        $data = $this->postData('data');
        $data['typeid'] = request()->post('typeid');
        if (Db::name('tags_list')->insert($data)) {
            return $this->renderSuccess('添加成功', url('transform.tags/list'));
        }
        return $this->renderError('添加失败');
    }



    public function edit_tags(){
        if (!$this->request->isAjax()) {
            return $this->renderError('错误请求');
        }

        $id =  request()->post('id');
        $name =  request()->post('name');
   
            if(!$id){
                $this->renderError('ID参数错误');
            }
            if(!$name){
                $this->renderError('请填写标签名称');
            }
       
        if (Db::name('tags_list')->where('id','=',$id)->update(['name'=>$name]) !==false ) {
            return $this->renderSuccess('修改成功', url('transform.tags/list'));
        }
        return $this->renderError('修改失败');
    }

    public function edit_type(){
        if (!$this->request->isAjax()) {
            return $this->renderError('错误请求');
        }
        $id =  request()->post('id');
        $name =  request()->post('name');

            if(!$id){
                $this->renderError('ID参数错误');
            }
            if(!$name){
                $this->renderError('请填写标签分类名称');
            }
       
        if (Db::name('tags_type')->where('id',$id)->update(['name'=>$name]) !==false ) {
            return $this->renderSuccess('修改成功', url('transform.tags/list'));
        }
        return $this->renderError('修改失败');
    }


    public function  delete_tags(){
        if (!$this->request->isAjax()) {
            return $this->renderError('错误请求');
        }
        $id =  request()->post('id');
        if(!$id){
            $this->renderError('ID参数错误');
        }
        if(Db::name('tags_list')->where('id',$id)->delete() !==false ) {
                return $this->renderSuccess('删除成功', url('transform.tags/list'));
        }
         return $this->renderError('删除失败');
    }

    public function  delete_type(){
        if (!$this->request->isAjax()) {
            return $this->renderError('错误请求');
        }
        $id =  request()->post('id');
        if(!$id){
            $this->renderError('ID参数错误');
        }

        $tags = Db::name('tags_list')->where('typeid',$id)->find();
        if($tags){
            return $this->renderError('当前分类下存在标签，请先删除！');
        }

        if(Db::name('tags_type')->where('id',$id)->delete() !==false ) {
                return $this->renderSuccess('删除成功', url('transform.tags/list'));
        }
         return $this->renderError('删除失败');
    }

}
